package com.util.net;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;

public class ApacheEmail {

	/**
	 * @param args
	 * @throws EmailException
	 */
	public static void main(String[] args) throws EmailException {
		// 创建一个Email附件
		EmailAttachment emailattachment = new EmailAttachment();
		emailattachment.setPath("d://1.jpg");
		emailattachment.setDisposition(EmailAttachment.ATTACHMENT);
		emailattachment.setDescription("This is Smile picture");
		emailattachment.setName("img.jpg");
		// 创建一个email
		MultiPartEmail multipartemail = new MultiPartEmail();
		multipartemail.setSslSmtpPort("465");
		multipartemail.setSSL(true);
		multipartemail.setHostName("smtp.gmail.com");
		multipartemail.addTo("345029460@qq.com", "name");// 对方
		multipartemail.setFrom("av@xu-ping.cn", "name");// 我方
		multipartemail.setAuthentication("av@xu-ping.cn", "aaaaaaaa");// 设置你邮箱和密码（这里我是把企业域名绑定QQ的企业邮箱里，所以可以用QQ的邮件服务器）
		multipartemail.setSubject("This is a att设置邮件服务器地址achment Email");// 标题
		multipartemail.setMsg("this a attachme设置邮件服务器地址nt Eamil Test");// 文本
		//multipartemail.setContent("this a attachme设置邮件服务器地址nt Eamil Test", "text/html;charset=GBK");
		// 添加附件
		multipartemail.attach(emailattachment);
		// 发送邮件
		multipartemail.send();
		System.out.println("The attachmentEmail send sucessful!!!");

	}

	/**
	 * 
	 * @return
	 */
	public static boolean sendMailByQqSmtp() {
		EmailAttachment emailattachment = new EmailAttachment();
		emailattachment.setPath("d://1.jpg");
		emailattachment.setDisposition(EmailAttachment.ATTACHMENT);
		emailattachment.setDescription("This is Smile picture");
		emailattachment.setName("img.jpg");
		// 创建一个email
		MultiPartEmail multipartemail = new MultiPartEmail();
		multipartemail.setHostName("smtp.qq.com"); // 设置邮件服务器地址
		try {
			multipartemail.addTo("345029460@qq.com", "name");
		} catch (EmailException e) {
			e.printStackTrace();
			return false;
		}
		try {
			multipartemail.setFrom("xuping@duoding.com", "name");
		} catch (EmailException e) {
			e.printStackTrace();
			return false;
		}
		multipartemail.setAuthentication("xuping@duoding.com", "zuaa@428");// 设置你邮箱和密码（这里我是把企业域名绑定QQ的企业邮箱里，所以可以用QQ的邮件服务器）
		multipartemail.setSubject("This is a att设置邮件服务器地址achment Email");// 标题
		try {
			multipartemail.setMsg("this a attachme设置邮件服务器地址nt Eamil Test");
		} catch (EmailException e) {
			e.printStackTrace();
			return false;
		}
		// 添加附件
		try {
			multipartemail.attach(emailattachment);
		} catch (EmailException e) {
			e.printStackTrace();
			return false;
		}
		// 发送邮件
		try {
			multipartemail.send();
		} catch (EmailException e) {
			e.printStackTrace();
			return false;
		}
		return true;

	}

}
